// 实现flat()方法
function flat1(arr) {
    let res = []
    for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i])) {
            res = res.concat(flat1(arr[i]))
        } else {
            res.push(arr[i])
        }
    }
    return res
}

function flat2(arr) {
    const res = arr.toString().split(',').map(item => +item)
    return res

}

function flat3(arr) {
    return arr.reduce((pre, cur) => {

        if (Array.isArray(cur)) {
            return pre.concat(flat3(cur))
        } else {
            return pre.concat(cur)
        }
    }, [])
}

// 测试
let arr = [1, 2, 3, [4, 5, 6, [7, 8, 9]]]
// console.log(flat1(arr))
// console.log(arr.flat(Infinity));
let str = '1,2,3,4'
// console.log(flat2(str));
console.log(flat3(arr));




